package com.d3works.apps.base.service;

import com.d3works.apps.base.db.ModelEmailLink;
import com.d3works.db.JPA;
import com.d3works.exception.D3worksException;
import com.d3works.meta.db.MetaModel;

public class ModelEmailLinkServiceImpl implements ModelEmailLinkService {

  @Override
  public boolean validateModelFields(ModelEmailLink modelEmailLink) throws D3worksException {

    try {
      MetaModel metaModel = modelEmailLink.getMetaModel();
      String fieldStr = modelEmailLink.getEmailField();
      String query = String.format("SELECT %s FROM %s", fieldStr, metaModel.getName());
      JPA.em().createQuery(query, String.class).getFirstResult();
    } catch (Exception e) {
      return false;
    }

    return true;
  }
}
